pub struct AvgCollection {
    pub list: Vec<u32>,
    pub avg: f64,
}

impl AvgCollection {
    pub fn add(&mut self, value: u32) {
        self.list.push(value);
        self.update_avg();
    }
    pub fn remove_last(&mut self) -> Option<u32> {
        let result = self.list.pop();
        match result {
            Some(value) => {
                self.update_avg();
                Some(value)
            }
            None => None,
        }
    }
    pub fn remove_index(&mut self, index: usize) -> Option<u32> {
        let len = self.list.len();
        if len <= index {
            return None;
        }
        let result = self.list.remove(index);
        self.update_avg();

        return Some(result);
    }

    pub fn get_avg(&self) -> f64 {
        self.avg
    }
    fn update_avg(&mut self) {
        let total: u32 = self.list.iter().sum();
        self.avg = total as f64 / self.list.len() as f64;
    }
}

fn avg_collection_test() {
    let mut avg_collection = AvgCollection {
        list: vec![3, 5, 9, 12],
        avg: 0 as f64,
    };
    avg_collection.add(123);
    println!("avg value is {}", avg_collection.get_avg());
    avg_collection.remove_last();
    println!("avg value is {}", avg_collection.get_avg());
    avg_collection.remove_index(1);
    println!("avg value is {}", avg_collection.get_avg());
    avg_collection.remove_index(5);
    println!("avg value is {}", avg_collection.get_avg());
}